package com.aaa.doctor.service.impl;

import com.aaa.doctor.dao.RegistrationDao;
import com.aaa.doctor.feign.UserFeign;
import com.aaa.doctor.service.RegistrationService;
import com.aaa.doctor.vo.RegistrationVo;
import com.aaa.entity.Registration;
import com.aaa.entity.User;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.deploy.security.SelectableSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import sun.net.TelnetInputStream;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class RegistrationServiceImpl implements RegistrationService {

    @Autowired
    private RegistrationDao registrationDao;

    /**
     * 查询所有挂号信息
     * @param currentPage
     * @param pageSize
     * @param registrationVo
     * @return
     */
    @Override
    public Result<IPage<Registration>> getAllInfo(Integer currentPage, Integer pageSize, RegistrationVo registrationVo) {
        IPage<Registration> iPage = new Page<>(currentPage, pageSize);
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(registrationVo.getDeptId())){
            wrapper.eq("dept_id",registrationVo.getDeptId());
        }
        if (StringUtils.hasText(registrationVo.getPatientName())){
            wrapper.like("patient_name",registrationVo.getPatientName());
        }
        if (StringUtils.hasText(registrationVo.getSchedulingType())){
            wrapper.eq("scheduling_type",registrationVo.getSchedulingType());
        }
        if (StringUtils.hasText(registrationVo.getSubsectionType())){
            wrapper.eq("subsection_type",registrationVo.getSubsectionType());
        }
        if (StringUtils.hasText(registrationVo.getRegistrationStatus())){
            wrapper.eq("registration_status",registrationVo.getRegistrationStatus());
        }
        if (StringUtils.hasText(registrationVo.getCreateTime())){
            wrapper.eq("create_time",registrationVo.getCreateTime());
        }
        IPage<Registration> registrationIPage = registrationDao.selectPage(iPage, wrapper);
        return new Result<>(200,"查询挂号信息成功",registrationIPage);
    }

    /**
     * 收费 退号
     * @param registrationId
     * @return
     */
    @Override
    public Result<String> updByrIdStatus(String registrationId) {
        String status1 = registrationDao.getStatus(registrationId);
        Registration registration = new Registration();
        if (status1.equals("0")){
            Integer status = registrationDao.updStatus(registrationId);
            if (status > 0){
                return new Result<>("收费成功！！！");
            }else {
                return new Result<>("收费失败！！！");
            }
        }else if (status1.equals("1")){
            Integer status = registrationDao.updWithdrawStatus(registrationId);
            if (status > 0){
                return new Result<>("退号成功！！！");
            }else {
                return new Result<>("退号失败！！！");
            }
        }else {
            return null;
        }
    }

    /**
     * 作废
     * 获取被收费人的registrationId
     * registrationId ---> 修改status
     * @param registrationId
     * @return
     */
    @Override
    public Result<String> updStatus(String registrationId) {
        Integer status = registrationDao.updVoidStatus(registrationId);
        if (status > 0){
            return new Result<>("作废成功！！！");
        }else {
            return new Result<>("作废失败！！！");
        }
    }

    /**
     * 查询待就诊的所有信息
     * @return
     */
    @Override
    public Result<List<Registration>> getWaitSchedulingType(String schedulingType) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) TokenUtils.getInfo(request.getHeader("token")).get("username");
        Long userId = userFeign.getByUsername(phone).getUserId();
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("scheduling_type",schedulingType);
        wrapper.eq("registration_status","1");
        List<Registration> registrations = registrationDao.selectList(wrapper);
        return new Result<>(200,"查询就诊中信息成功",registrations);
    }

    /**
     * 查询就诊中的所有信息
     * @param schedulingType
     * @return
     */
    @Override
    public Result<List<Registration>> getVisitingSchedulingType(String schedulingType) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) TokenUtils.getInfo(request.getHeader("token")).get("username");
        Long userId = userFeign.getByUsername(phone).getUserId();
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("scheduling_type",schedulingType);
        wrapper.eq("registration_status","2");
        List<Registration> registrations = registrationDao.selectList(wrapper);
        return new Result<>(200,"查询就诊中信息成功",registrations);
    }

    /**
     * 查询就诊完成的所有信息
     * @param schedulingType
     * @return
     */
    @Override
    public Result<List<Registration>> getFinishSchedulingType(String schedulingType) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) TokenUtils.getInfo(request.getHeader("token")).get("username");
        Long userId = userFeign.getByUsername(phone).getUserId();
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("scheduling_type",schedulingType);
        wrapper.eq("registration_status","3");
        List<Registration> registrations = registrationDao.selectList(wrapper);
        return new Result<>(200,"查询就诊完成信息成功",registrations);
    }

    @Autowired
    private UserFeign userFeign;

    /**
     * 接诊，更新挂号的状态为就诊中
     * @param registrationId
     * @return
     */
    @Override
    public Result<String> updSchedulingType(String registrationId) {
        //1.修改挂号状态
        Integer status = registrationDao.updSchedulingType(registrationId);
        if (status > 0 ){
            return new Result<>("接诊成功");
        }else {
            return new Result<>("接诊失败");
        }

    }

    /**
     * 新开就诊页面
     * 完成就诊 -- 更改状态为就诊完成
     * @param registrationId
     * @return
     */
    @Override
    public Result<String> updvisitCompleteSchedulingType(String registrationId) {
        Integer status = registrationDao.updvisitCompleteSchedulingType(registrationId);
        if (status > 0 ){
            return new Result<>("接诊成功");
        }else {
            return new Result<>("接诊失败");
        }
    }


    /**
     * 查询收费是否成功
     * @param registrationId
     * @return
     */
    @Override
    public Result  getRegId(String registrationId) {
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("registration_id",registrationId);
        Registration registration = registrationDao.selectOne(wrapper);
        if (registration.getRegistrationStatus().equals("1")){
            return new Result("收费成功");
        } else{
            return new Result("没收费");
        }
    }

}
